<?php
/**
 * 住所検索用関数<br />
 *
 * @package apamanshop
 * @author　
 * @copyright　
 */
class Search_Model_Selcyo extends Apm_Model_Base{

	/**
	* 町名一覧データ取得
	*/
	public function getCyoData($params)
	{
		$db_params = array();
		$sql = $this->generateCyoSql($params,$db_params);
		return $this->dbGetList($sql,$db_params);
	}

	/**
	* 町名一覧データ取得（リクエスト用）
	*/
	public function getRequestCyoData($params)
	{
		$db_params = array();
		$sql = $this->generateRequestCyoSql($params,$db_params);
		return $this->dbGetList($sql,$db_params);
	}

	/**
	* 町名一覧データ取得（SEOリンク用）
	*/
	public function getMoreCyoData($params, $pageCount=90, $page=1, $pageStart=0)
	{
		$db_params = array();
		$sql = $this->generateMoreCyoSql($params,$db_params,$pageCount,$page,$pageStart);
		return $this->dbGetList($sql,$db_params);
	}

	/**
	* 町名一覧データ件数取得（SEOリンク用）
	*/
	public function getMoreCyoDataCount($params)
	{
		$db_params = array();
		$sql = $this->generateMoreCyoCountSql($params,$db_params);
		return $this->dbGetData($sql,$db_params);
	}

	/**
	* 市区町村コードから市区町村名取得
	*/
	public function getShikucyosonNameByShikucyosonCd($params)
    {
        $db_params = array();        
        $sql = $this->generateShikucyosonNameByShikucyosonCdSql($params, $db_params);  
        return $this->dbGetList($sql, $db_params);
    }

	/**
	* 町コードから町名取得
	*/
	public function getCyoNameByCyoCd($params)
    {
        $db_params = array();
        $sql = $this->generateCyoNameByCyoCdSql($params, $db_params);
        return $this->dbGetList($sql, $db_params);
    }

   /**
	* 町名一覧データを表示用に変換
	*
	* @access	public
	* @param	array ※参照渡し
	* @return	void
	*/
	public function convertCyoData(&$data)
	{
		$data['Mapping'] = array();

		$beforeShikuchosonCd = null;

		foreach ($data['seltown'] as $townData)
		{
			$shikuchosonCd = $townData['SHIKUCHOUSON_CD'];

			// 市区町村が異なる場合
			if (array_key_exists($shikuchosonCd, $data['Mapping']) == false)
			{
				$data['Mapping']["$shikuchosonCd"]['SHIKUCHOUSON_NAME'] = $townData['SHIKUCHOUSON'];
				$data['Mapping']["$shikuchosonCd"]['SHIKUCHOUSON_HEYA_CNT'] = $townData['SHIKUCHOUSON_HEYA_CNT'];
				$data['Mapping']["$shikuchosonCd"][] = array();

			}

			$data['Mapping']["$shikuchosonCd"]['TOWN_DATA'][] = array(
			'CHOUMEI'			=> $townData['CHOUMEI'],
			'CHOUMEI_CD'		=> $townData['CHOUMEI_CD'],
			'REC_CNT'			=> $townData['REC_CNT'],
			'CHOUMEI_KANA'      => $townData['KANA']
			);
		}
	}


    /**
     * 町名データ検索用SQL文の取得
     *
     * 町名一覧データ検索用SQL文の取得を行う
     * @access	public
     * @param	array
     * @param	array
     * @return	string 町名データ検索用SQL
     */
    public function generateCyoSql($params,&$db_params)
    {

		//バインド変数のセット		
		// データ区分
		$db_params['dataKubun'] = 0;	
		if(!empty($params['shikireizero']) && $params['shikireizero'] == "on"){
			$db_params['dataKubun'] = 1;
		}else if(!empty($params['shikikin_nashi']) && $params['shikikin_nashi'] == "on" &&
					(!empty($params['reikin_nashi']) && $params['reikin_nashi'] == "on")){
			$db_params['dataKubun'] = 3;
		}else if(!empty($params['shikikin_nashi']) && $params['shikikin_nashi'] == "on"){
			$db_params['dataKubun'] = 4;
		}else if(!empty($params['reikin_nashi']) && $params['reikin_nashi'] == "on"){
			$db_params['dataKubun'] = 5;
		}else if(!empty($params['SrhKbn']) && $params['SrhKbn'] == Apm_Config_Search::SRHKBN_SYATAKU){
			$db_params['dataKubun'] = 2;
		}else{
			$db_params['dataKubun'] = 0;
		}
	
		$sql = "";
		$sql .= "SELECT";
		$sql .= "         T1.T55_CHOUMEI_CNT AS REC_CNT";
		$sql .= "        ,T1.T55_TODOUFUKEN_CD || T1.T55_SHIKUCHOUSON_CD AS SHIKUCHOUSON_CD";
		$sql .= "        ,T1.M01_TODOUFUKEN || T1.M01_SHIKUCHOUSON AS SHIKUCHOUSON";
		$sql .= "        ,T1.T55_TODOUFUKEN_CD || T1.T55_SHIKUCHOUSON_CD || T1.T55_CHOUMEI_CD AS CHOUMEI_CD";
		$sql .= "        ,to_single_byte(T1.T55_CHOUMEI) AS CHOUMEI";
		$sql .= "        ,T1.M01_CHOUMEI_KANA AS KANA";
		$sql .= "        ,T2.SHIKUCHOUSON_HEYA_CNT";
		$sql .= "    FROM";
		$sql .= "        (";
		$sql .= "            SELECT";
		$sql .= "                     T55_TODOUFUKEN_CD";
		$sql .= "                    ,T55_SHIKUCHOUSON_CD";
		$sql .= "                    ,T55_CHOUMEI_CD";
		$sql .= "                    ,M01_CHOUMEI_KANA";
		$sql .= "                    ,M01_TODOUFUKEN";
		$sql .= "                    ,M01_SHIKUCHOUSON";
		$sql .= "                    ,T55_CHOUMEI";
		$sql .= "                    ,T55_CHOUMEI_CNT";
		$sql .= "                FROM";
		$sql .= "                     T55CHOUMEI_SUM";
		$sql .= "                    ,M01JYUUSHO";
		$sql .= "                WHERE";
		$sql .= 				 Apm_SqlUtil::createWhereTown("PrefCd1","TwonCd1","T55_TODOUFUKEN_CD","T55_SHIKUCHOUSON_CD",$params['CityCd'],$db_params);		
		//$sql .= 				  $this->createWhereTown("T55_TODOUFUKEN_CD","T55_SHIKUCHOUSON_CD",$params['CityCd'],"");			
		$sql .= "   			 AND T55_DATA_KUBUN = :dataKubun ";
		$sql .= "                AND T55_TODOUFUKEN_CD = M01_TODOUFUKEN_CD";
		$sql .= "                AND T55_SHIKUCHOUSON_CD = M01_SHIKUCHOUSON_cd";
		$sql .= "                AND T55_CHOUMEI_CD = M01_CHOUMEI_CD";
		$sql .= "        ) T1";
		$sql .= "        ,(";
		$sql .= "            SELECT";
		$sql .= "                     T57_SHIKUCHOUSON_CNT AS SHIKUCHOUSON_HEYA_CNT";
		$sql .= "                    ,T57_TODOUFUKEN_CD";
		$sql .= "                    ,T57_SHIKUCHOUSON_CD AS T57_SHIKUCHOUSON_CD";
		$sql .= "                FROM";
		$sql .= "                     T57SHIKUCHOUSON_SUM";
		$sql .= "                WHERE";
		$sql .= 				    Apm_SqlUtil::createWhereTown("PrefCd2","TwonCd2","T57_TODOUFUKEN_CD","T57_SHIKUCHOUSON_CD",$params['CityCd'],$db_params);		
		//$sql .=						$this->createWhereTown("T57_TODOUFUKEN_CD","T57_SHIKUCHOUSON_CD",$params['CityCd'],"");	
		$sql .= "   				 AND T57_DATA_KUBUN = :dataKubun ";
		$sql .= "        ) T2";
		$sql .= "    WHERE";
		$sql .= "        T1.T55_TODOUFUKEN_CD = T2.T57_TODOUFUKEN_CD";
		$sql .= "    AND T1.T55_SHIKUCHOUSON_CD = T2.T57_SHIKUCHOUSON_CD";
		$sql .= "    GROUP BY";
		$sql .= "         T1.T55_CHOUMEI_CNT";
		$sql .= "        ,T1.T55_TODOUFUKEN_CD";
		$sql .= "        ,T1.T55_SHIKUCHOUSON_CD";
		$sql .= "        ,T1.T55_CHOUMEI_CD";
		$sql .= "        ,T1.T55_CHOUMEI";
		$sql .= "        ,T1.M01_CHOUMEI_KANA";
		$sql .= "        ,T1.M01_TODOUFUKEN";
		$sql .= "        ,T1.M01_SHIKUCHOUSON";
		$sql .= "        ,T2.SHIKUCHOUSON_HEYA_CNT";
		$sql .= "    ORDER BY";
		$sql .= "         SHIKUCHOUSON_CD ASC";
		$sql .= "        ,KANA ASC";

        return $sql;
    }

    /**
     * 町名データ検索用SQL文の取得
     *
     * 町名一覧データ検索用SQL文の取得を行う
     * @access	public
     * @param	array
     * @param	array
     * @return	string 町名データ検索用SQL
     */
    public function generateRequestCyoSql($params, &$db_params)
    {
		$sql = "";
		$sql .= " SELECT";
		$sql .= "      -1 AS REC_CNT";
		$sql .= "     ,M01_TODOUFUKEN_CD || M01_SHIKUCHOUSON_CD AS SHIKUCHOUSON_CD";
		$sql .= "     ,M01_TODOUFUKEN || M01_SHIKUCHOUSON AS SHIKUCHOUSON";
		$sql .= "     ,M01_TODOUFUKEN_CD || M01_SHIKUCHOUSON_CD || M01_CHOUMEI_CD AS CHOUMEI_CD";
		$sql .= "     ,to_single_byte(M01_CHOUMEI) AS CHOUMEI";
		$sql .= "     ,M01_CHOUMEI_KANA AS KANA";
		$sql .= "     ,-1 AS SHIKUCHOUSON_HEYA_CNT";
		$sql .= "   FROM";
		$sql .= "      M01JYUUSHO";
		$sql .= "  WHERE";
		$sql .=          $this->createWhereTown_prm("M01_TODOUFUKEN_CD", "M01_SHIKUCHOUSON_CD", $params['CityCd'], $db_params);
		$sql .= "    AND trim(M01_ABOLISH_YM) is null";
		$sql .= "  GROUP BY";
		$sql .= "      M01_TODOUFUKEN_CD";
		$sql .= "     ,M01_SHIKUCHOUSON_CD";
		$sql .= "     ,M01_CHOUMEI_CD";
		$sql .= "     ,M01_TODOUFUKEN";
		$sql .= "     ,M01_SHIKUCHOUSON";
		$sql .= "     ,M01_CHOUMEI";
		$sql .= "     ,M01_CHOUMEI_KANA";
		$sql .= "  ORDER BY";
		$sql .= "      M01_SHIKUCHOUSON_CD asc";
		$sql .= "     ,M01_CHOUMEI_KANA ASC";

        return $sql;
    }

    /**
     * 町名データ検索用SQL文の取得
     *
     * 町名一覧データ検索用SQL文の取得を行う
     * @access	public
     * @param	array
     * @param	array
     * @return	string 町名データ検索用SQL
     */
    public function generateMoreCyoSql($params,&$db_params, $pageCount, $page, $pageStart=0)
    {
		$sql  = "";
		$sql .= "SELECT * ";
		$sql .= "  FROM (";
		$sql .= "    SELECT ";
		$sql .= "         REC_CNT";
		$sql .= "        ,SHIKUCHOUSON_CD";
		$sql .= "        ,SHIKUCHOUSON";
		$sql .= "        ,CHOUMEI_CD";
		$sql .= "        ,CHOUMEI";
		$sql .= "        ,KANA";
		$sql .= "        ,SHIKUCHOUSON_HEYA_CNT";
		$sql .= "        ,rownum as num";
		$sql .= "      FROM (";
		$sql .= $this->generateCyoSql($params, $db_params);
		$sql .= "     )";
		$sql .= " )";
		$sql .= " WHERE num between ".(($page-1)*$pageCount+1+$pageStart)." and ".($page*$pageCount+$pageStart);

        return $sql;
    }

    /**
     * 町名データ件数検索用SQL文の取得
     *
     * 町名一覧データ件数検索用SQL文の取得を行う
     * @access	public
     * @param	array
     * @param	array
     * @return	string 町名データ件数検索用SQL
     */
	public function generateMoreCyoCountSql($params,&$db_params)
	{
		$sql  = "";
		$sql .= "SELECT count(*) as CNT ";
		$sql .= "  FROM (";
		$sql .= $this->generateCyoSql($params, $db_params);
		$sql .= " )";
		return $sql;
	}

    /**
     * 市区町村名取得用SQL文の取得
     *
     * 市区町村名取得SQL文の取得を行う
     * @access	public
     * @param	array
     * @param	array
     * @return	string 市区町村名取得SQL
     */
    public function generateShikucyosonNameByShikucyosonCdSql($params, &$db_params)
    {

		$sql = "";
		$sql .= "SELECT ";
		$sql .= "    distinct M01_SHIKUCHOUSON_CD ";
		$sql .= "    ,M01_TODOUFUKEN_CD ";
		$sql .= "    ,to_single_byte(M01_SHIKUCHOUSON) M01_SHIKUCHOUSON ";
		$sql .= "    ,M01_SHIKUCHOUSON_ALIAS ";
		$sql .= "FROM ";
		$sql .= "    M01JYUUSHO ";
		$sql .= "WHERE ";
		$sql .=      $this->createWhereTown("M01_TODOUFUKEN_CD","M01_SHIKUCHOUSON_CD",$params['CityCd'],"");
		$sql .= " AND M01_SHIKUCHOUSON_ALIAS IS NOT NULL ";
        $sql .= "ORDER BY ";
        $sql .= "    M01_SHIKUCHOUSON_CD ";
        
        return $sql;
    }

    /**
     * 町名取得用SQL文の取得
     *
     * 町名取得SQL文の取得を行う
     * @access	public
     * @param	array
     * @param	array
     * @return	string 町名取得SQL
     */
    public function generateCyoNameByCyoCdSql($params, &$db_params)
    {

		$sql = "";
		$sql .= "SELECT ";
		$sql .= "    distinct M01_CHOUMEI_CD ";
		$sql .= "    ,to_single_byte(M01_CHOUMEI) AS M01_CHOUMEI";
		$sql .= "    ,M01_SHIKUCHOUSON_CD";
		$sql .= "    ,decode(nvl(M44_SITEITOSI_CD,0),20,to_single_byte(M01_CHOUMEI),M01_SHIKUCHOUSON || to_single_byte(M01_CHOUMEI)) as CHOUMEI_ALIAS ";
		$sql .= "FROM ";
		$sql .= "    M01JYUUSHO ";
		$sql .= "   ,M44SITEITOSI_SIKU ";
		$sql .= "WHERE ";
		$sql .=      $this->createWhereFromPrefCdToTownCd("M01_TODOUFUKEN_CD","M01_SHIKUCHOUSON_CD","M01_CHOUMEI_CD",$params['TownCd'],"");
		$sql .= "  AND M01_TODOUFUKEN_CD = M44_TODOUFUKEN_CD(+) ";
		$sql .= "  AND M01_SHIKUCHOUSON_CD = M44_SHIKUCHOUSON_CD(+) ";
        $sql .= "ORDER BY ";
        $sql .= "    M01_CHOUMEI_CD ";

        return $sql;
    }

   /**
	* 都道府県・市区町村コードWhere句作成
	*
	* @access	public
	* @param	string
	* @param	string
	* @param	array
	* @param	string
	* @return	void
	*/
	public function createWhereTown($colums1,$colums2,$params,$option)
	{
		// コード数分ループ
		for($i=0;$i<count($params);$i++){

			if($i == 0){
				$sql = " (( ".$colums1." = '".substr($params[$i],0,2)."' AND ".$colums2." = '".substr($params[$i],2,3)."' ".$option.")";
			}else{
				$sql.= " OR (".$colums1." = '".substr($params[$i],0,2)."' AND ".$colums2." = '".substr($params[$i],2,3)."' ".$option.")";
			}
		}

		$sql .= ")";

		return $sql;
    }

   /**
	* 都道府県・市区町村コードWhere句作成
	*
	* @access	public
	* @param	string
	* @param	string
	* @param	array
	* @param	string
	* @return	void
	*/
	public function createWhereTown_prm($colums1, $colums2, $params, &$db_params, $option="")
	{
		// WHERE句生成
		$strWhere = "";
		for ($i= 0; $i<count($params); $i++)
		{
			// OR句の取得
			$strOr = ($i != 0) ? " OR" : "";
			// パラメータ
			$db_params['strCityCd_'.$i] = $params[$i];

			$strWhere .= $strOr." ( " . $colums1 . " = substr( :strCityCd_".$i." ,1,2)";
			$strWhere .=      " AND " . $colums2 . " = substr( :strCityCd_".$i." ,3,3)";
			$strWhere .= $option . " ) ";
		}

		return " (" . $strWhere . ") ";
    }

   /**
	* 都道府県・市区町村・町コードWhere句作成
	*
	* @access	public
	* @param	string
	* @param	string
	* @param	array
	* @param	string
	* @return	void
	*/
	public function createWhereFromPrefCdToTownCd($colums1,$colums2,$colums3,$params,$option)
	{
		// コード数分ループ
		for($i=0;$i<count($params);$i++){

			if($i == 0){
				$sql .= " ( (    ".$colums1." = '".substr($params[$i],0,2)."'";
				$sql .= "    AND ".$colums2." = '".substr($params[$i],2,3)."' ";
				$sql .= "    AND ".$colums3." = '".substr($params[$i],5,3)."' ".$option.")";
			}else{
				$sql .= " OR (    ".$colums1." = '".substr($params[$i],0,2)."'";
				$sql .= "     AND ".$colums2." = '".substr($params[$i],2,3)."' ";
				$sql .= "     AND ".$colums3." = '".substr($params[$i],5,3)."' ".$option.")";
			}
		}

		$sql .= ")";

		return $sql;
    }

    /**
     * 集計テーブルの編集
     *
     * @access    public
     * @param     array
     * @return    void
     */
    public function editSum($params)
    {
        $db = $this->getDbConnection();
        $result = array();
        $this->startTransaction($db);
        try {
            // DELETE対象取得
            $db_params = array();
            $sql = $this->generateGetDelSumSql($params, $db_params);
            $result = $this->dbGetList($sql, $db_params);
            if (!empty($result)) {
                // 条件生成
                $where = $this->createWhereEditSum($result, "T55");
                // DELETE文生成
                $sql = $this->generateDelSumSql($where);
                // 削除
                $this->dbExecQuery($sql, array(), $db);
            }

            // INSERT対象取得
            $db_params = array();
            $sql = $this->generateGetInsSumSql($params, $db_params);
            $result = $this->dbGetList($sql, $db_params);
            if (!empty($result)) {
                // 条件生成
                $where = $this->createWhereEditSum($result, "T03");
                // INSERT文生成
                $sql = $this->generateInsSumSql($where);
                // 追加
                $this->dbExecQuery($sql, array(), $db);
            }
            $this->commitTransaction($db);
        } catch (Exception $e) {
            $this->rollbackTransaction($db);
            __log_debug(__FILE__, __LINE__, __METHOD__, $e->getMessage());
        }
        $db->closeConnection();

        return;

    }

    /**
     * 集計テーブルの追加検索用SQL取得
     *
     * @access    private
     * @return    string
     */
    private function generateGetInsSumSql($params, &$db_params)
    {

        $sql  = "";
        $sql .= "SELECT";
        $sql .= "     M01_TODOUFUKEN_CD AS TODOUFUKEN_CD";
        $sql .= "    ,M01_SHIKUCHOUSON_CD AS SHIKUCHOUSON_CD";
        $sql .= "  FROM";
        $sql .= "    M01JYUUSHO M01";
        $sql .= " WHERE";
        $sql .=      $this->createWhereTown_prm("M01.M01_TODOUFUKEN_CD", "M01.M01_SHIKUCHOUSON_CD", $params['CityCd'], $db_params);
        $sql .= "    AND NOT EXISTS (";
        $sql .= "        SELECT";
        $sql .= "            'X'";
        $sql .= "          FROM";
        $sql .= "            T55CHOUMEI_SUM T55";
        $sql .= "         WHERE";
        $sql .= "                M01.M01_TODOUFUKEN_CD = T55.T55_TODOUFUKEN_CD";
        $sql .= "            AND M01.M01_SHIKUCHOUSON_CD = T55.T55_SHIKUCHOUSON_CD";
        $sql .= "            AND T55.T55_DATA_KUBUN = 0";
        $sql .= "    )";
        $sql .= " GROUP BY";
        $sql .= "     M01_TODOUFUKEN_CD";
        $sql .= "    ,M01_SHIKUCHOUSON_CD";

        return $sql;
    }

    /**
     * 集計テーブルの追加用SQL取得
     *
     * @access    private
     * @params    array
     * @return    string
     */
    private function generateInsSumSql($params)
    {

        $sql  = "";
        $sql .= "INSERT INTO T55CHOUMEI_SUM";
        $sql .= "(";
        $sql .= "     T55_TODOUFUKEN_CD";
        $sql .= "    ,T55_SHIKUCHOUSON_CD";
        $sql .= "    ,T55_CHOUMEI_CD";
        $sql .= "    ,T55_CHOUMEI";
        $sql .= "    ,T55_CHOUMEI_CNT";
        $sql .= "    ,T55_KOUSHIN";
        $sql .= "    ,T55_DATA_KUBUN";
        $sql .= ") ";
        $sql .= "SELECT";
        $sql .= "     T03_TODOUFUKEN_CD";
        $sql .= "    ,T03_SHIKUCHOUSON_CD";
        $sql .= "    ,T03_CHOUMEI_CD";
        $sql .= "    ,T03_CHOUMEI";
        $sql .= "    ,count(T03_ORIGINAL_HEYA_CD01) AS HEYA_CNT";
        $sql .= "    ,sysdate AS KOSHIN";
        $sql .= "    ,0 AS DATA_KBN";
        $sql .= "  FROM (";
        $sql .= "    SELECT";
        $sql .= "         T03_TODOUFUKEN_CD";
        $sql .= "        ,T03_SHIKUCHOUSON_CD";
        $sql .= "        ,T03_CHOUMEI_CD";
        $sql .= "        ,T03_CHOUMEI";
        $sql .= "        ,T03_ORIGINAL_HEYA_CD01";
        $sql .= "      FROM";
        $sql .= "         T03WEB_HEYA";
        $sql .= "     WHERE";
        $sql .=           $params;
        $sql .= "     GROUP BY";
        $sql .= "         T03_TODOUFUKEN_CD";
        $sql .= "        ,T03_SHIKUCHOUSON_CD";
        $sql .= "        ,T03_CHOUMEI_CD";
        $sql .= "        ,T03_CHOUMEI";
        $sql .= "        ,T03_ORIGINAL_HEYA_CD01";
        $sql .= "    )";
        $sql .= " GROUP BY";
        $sql .= "     T03_TODOUFUKEN_CD";
        $sql .= "    ,T03_SHIKUCHOUSON_CD";
        $sql .= "    ,T03_CHOUMEI_CD";
        $sql .= "    ,T03_CHOUMEI";

        return $sql;
    }

    /**
     * 集計テーブルの削除検索用SQL取得
     *
     * @access    private
     * @return    string
     */
    private function generateGetDelSumSql($params, &$db_params)
    {

        $sql  = "";
        $sql .= "SELECT";
        $sql .= "     M01_TODOUFUKEN_CD AS TODOUFUKEN_CD";
        $sql .= "    ,M01_SHIKUCHOUSON_CD AS SHIKUCHOUSON_CD";
        $sql .= "  FROM";
        $sql .= "     M01JYUUSHO M01";
        $sql .= " WHERE";
        $sql .=      $this->createWhereTown_prm("M01.M01_TODOUFUKEN_CD", "M01.M01_SHIKUCHOUSON_CD", $params['CityCd'], $db_params);
        $sql .= "    AND EXISTS (";
        $sql .= "        SELECT";
        $sql .= "            'X'";
        $sql .= "          FROM";
        $sql .= "            T55CHOUMEI_SUM T55";
        $sql .= "         WHERE";
        $sql .= "                M01.M01_TODOUFUKEN_CD = T55.T55_TODOUFUKEN_CD";
        $sql .= "            AND M01.M01_SHIKUCHOUSON_CD = T55.T55_SHIKUCHOUSON_CD";
        $sql .= "            AND T55.T55_DATA_KUBUN = 0";
        $sql .= "            AND T55.T55_KOUSHIN < SYSDATE - 30 / 1440";
        $sql .= "    )";
        $sql .= " GROUP BY";
        $sql .= "     M01_TODOUFUKEN_CD";
        $sql .= "    ,M01_SHIKUCHOUSON_CD";

        return $sql;
    }

    /**
     * 集計テーブルの削除用SQL取得
     *
     * @access    private
     * @params    array
     * @return    string
     */
    private function generateDelSumSql($params)
    {

        $sql  = "";
        $sql .= "DELETE FROM";
        $sql .= "    T55CHOUMEI_SUM";
        $sql .= " WHERE";
        $sql .=      $params;
        $sql .= "    AND T55_DATA_KUBUN = 0";

        return $sql;
    }

    /**
     * 集計テーブル編集時のWHERE句生成
     *
     * @access    private
     * @params    array
     * @return    string
     */
    private function createWhereEditSum($result, $tblKbn)
    {

        $sql = "";
        foreach ($result as $city)
        {
            $sql .= " OR (" . $tblKbn . "_TODOUFUKEN_CD = '" . $city['TODOUFUKEN_CD'] . "' AND " . $tblKbn . "_SHIKUCHOUSON_CD = '" . $city['SHIKUCHOUSON_CD'] . "')";
        }
        if ($sql != "")
        {
            $sql = " (".substr($sql, 3, strlen($sql)).") ";
        }

        return $sql;
    }

}

?>